<?php
include 'ini.php';
include 'header.php';

require 'lib/Stripe.php';

if(!isset($_POST['packageType']) || !isset($_POST['testType']))
{
    if( isset($_COOKIE['typeOfTest']) && isset($_COOKIE['amountOfTests']))
    {
        $packageType    = $_COOKIE['amountOfTests'];
        $testType       = $_COOKIE['typeOfTest'];
    }
    else
    {    
        echo '<center>Uh oh... looks like something went wrong.  Click <a href="index.php">here</a> to go back to the home page.</center>';
        exit();
    }
}
else 
{
    $packageType    = $_POST['packageType'];
    $testType       = $_POST['testType'];
    
    if($packageType == 1)
    {
        $amount = '4.99';
    }
    if($packageType == 3)
    {
        $amount = '11.99';
    }
    if($packageType == 5)
    {
        $amount = '19.99';
    }
}

if (isset($_POST['email']))
{
    Stripe::setApiKey("sk_0ImkhuyewxkmXGvLg9TwJ862lOYAW");
    $error = '';
    $success = '';
    try
    {
        if (!isset($_POST['stripeToken']))
            throw new Exception("The Stripe Token was not generated correctly");
        
        $packageAmount = intval($_POST['p_a']);
        
        // set the correct amount to charge based on how many tests the user wants
        if($packageAmount == 1)
        {
            $amount = 499;
        }
        if($packageAmount == 3)
        {
            $amount = 1199;
        }
        if($packageAmount == 5)
        {
            $amount = 1999;
        }
        
        Stripe_Charge::create(array("amount"        => $amount,
                                    "currency"      => "usd",
                                    "description"   => $_POST['email'],
                                    "card"          => $_POST['stripeToken']));
        
        $success    = 'Your payment was successful.';
	
        /*mysql_connect('localhost','root','awesome0');
	mysql_select_db('studyguide');

	  
	date_default_timezone_set('America/New_York');
	$dated = date('m/d/y');   				
	mysql_query("INSERT INTO `main` VALUES ('', '$name', '$dated', '$code')");
	$_SESSION['id']= $name;
	mailto($name);
	header('Location: purchase_confirmation.php');
        */
	
  }
  catch (Exception $e)
  {
    $error = $e->getMessage();
    echo $error;
  }
}
?>
    
    <script type="text/javascript" src="https://js.stripe.com/v1/"></script>
    <script type="text/javascript">
    // This identifies your website in the createToken call below
    Stripe.setPublishableKey('pk_WIwQA3xoR5pn5zq9BesKT3BCGUGMS');

        function stripeResponseHandler(status, response) 
        {
            if (response.error) {
                // re-enable the submit button
                //$('.submit-button').removeAttr("disabled");
                // show the errors on the form

                $("#payment-errors").html(response.error.message);
            } else {
                var form$ = $("#payment-form");
                // token contains id, last4, and card type
                var token = response['id'];
                // insert the token into the form so it gets submitted to the server
                form$.append("<input type='hidden' name='stripeToken' value='" + token + "' />");
                // and submit
                form$.get(0).submit();
            }
        }   
        </script>

<div class="col-lg-4 col-lg-offset-1">
<form id="payment-form" method="post" action="">    
  <fieldset>
      <legend>Account Information <div style="clear: both;"></div><div style="font-size: 11px;color: #888888;">(remember this info so you can score your tests)</div></legend>
    <div class="form-group">
      <label for="exampleInputEmail">Email address</label>
      <input type="text" class="form-control" id="email" name="email" placeholder="Enter email">
    </div>
    <div class="form-group" id="passwordDiv">
      <label for="exampleInputPassword">Password</label>
      <input type="password" class="form-control" id="password" name="password" placeholder="Password">
    </div>
      <div class="form-group" id="confirmPassDiv">
      <label for="exampleInputPassword">Confirm Password</label>
      <input onblur="checkPassword();" type="password" class="form-control" id="confirmPassword" name="confirmPassword" placeholder="Password">
    </div>
  </fieldset>
   
    <fieldset>
      <legend>Billing Information<div style="clear: both;"></div></legend>
    <div class="form-group">
      <label for="exampleInputEmail">Credit Card Number</label>
      <input type="text" class="form-control" id="cc" placeholder="CC Number">
    </div>
      <div style="width: 150px; float: left;">
    <div class="form-group">
      <label for="exampleInputPassword">Expires <span style="font-size: 11px;color: #888888; font-weight: 400;">(mm / yy)</span></label>
      <div style="clear: both;"></div>
      <div style="float: left; width: 50px;">
      <input type="text" size="2" class="form-control" id="expirationMonth" placeholder="MM">
      </div>
      <div style="float: left; margin: 10px 5px 0 5px;">/</div>
      <div style="float: left; width: 50px;">
      <input type="text" size="2" class="form-control" id="expirationYear" placeholder="YY">
      </div>
    </div>
      </div>
      <div style="width: 100px; float: right;">
      <div class="form-group">
      <label for="exampleInputPassword">Card Code</label>
      <input type="password" class="form-control" id="cc-code" placeholder="">
    </div>
      </div>
      <div style="clear: both;"></div>
      <br>
      <div id="payment-errors"></div>
      <input type="hidden" name="p_a" value="<?php echo $packageType;?>"/>
    <button type="submit" class="btn btn-primary btn-block" onclick="">Submit Payment</button>
  </fieldset>
</form>
</div>
<div class="col-lg-4 col-lg-offset-2" >
    
    <div class="col-lg-10 well">
        <b>Order Information: </b><br><?php echo $testType." : ".$packageType;?> practice test(s)
            <br>
            
            <div style='font-size: 14pt; border-top: solid thin #555555; width: 100%; margin-top: 15px'">Total: <b> $ <?php echo $amount;?></b></div> 
        
    </div>
    <div class="col-lg-10 " style="text-align:justify;text-justify:inter-word;">
        Your tests will be available in your account upon payment. (Login details and download instructions will be sent to you within one or two minutes.)
    </div>
</div>